package singleton;


/**
 * @desc 双重锁检查线程安全的懒加载 单例模式
 * 通过volatile防止指令重排，保证线程安全；
 * synchronzied(class)保证类层级上的同步锁
 * @since 2023/11/6 11:02
*/
public class SingleInstance {

    private static volatile SingleInstance instance;

    private String name;


    public SingleInstance(String name) {
        this.name = name;
    }

    public static SingleInstance getInstance(String name) {
        if (instance == null) {
            synchronized (SingleInstance.class) {
                if (instance == null) {
                    instance = new SingleInstance(name);
                }
            }
        }
        return instance;
    }

    public void print(){
        System.out.println(this.name);
    }

    public void setName(String name) {
        this.name = name;
    }
}
